#version 320 es
#ifdef GL_ES
	precision highp float;
#endif
out vec4 FragColor;

in vec3 position;
in float Lx;
in float Rx;

struct Arrow {
	float zStart;
	float zEnd;
	float edgeW;
};
uniform Arrow arrow;

uniform vec4 color;

const float EdgeAlphaIncrementRatio = 0.25;
const float FadeOutTopPercent = 0.3;
const float GradientFirstPercent = 0.3;
const vec3 White = vec3(1.0, 1.0, 1.0);

void main() {
	vec3 _color;
	/** gradient: white -> color -> white */
	float t = (arrow.zStart - position.z) / (arrow.zStart - arrow.zEnd);
	if (t < GradientFirstPercent)// first T% : white->color
		_color = mix(White, color.rgb, t / GradientFirstPercent);
	else// left 1-T% : color->white
		_color = mix(color.rgb, White, (t - GradientFirstPercent) / (1.0 - GradientFirstPercent));
	
	float maxBaseAlpha = color.a;
	if (abs(position.x - Lx) < arrow.edgeW || abs(position.x - Rx) < arrow.edgeW)
		maxBaseAlpha += color.a * EdgeAlphaIncrementRatio;

	float alpha;
	if (t < 1.0 - FadeOutTopPercent)
		alpha = mix(maxBaseAlpha, 0.0, t);
	else {
		float startAlpha = mix(maxBaseAlpha, 0.0, FadeOutTopPercent);
		float f = (t - FadeOutTopPercent) / (1.0 - FadeOutTopPercent);
		f = smoothstep(0.0, 1.0, f);
		alpha = (1.0 - f) * startAlpha;
	}

	FragColor = vec4(_color, alpha);
}